<template>
  <div class="admin-navbar">
    <div class="logo">
      <h1>校园二手书交易平台</h1>
      <span class="admin-badge">管理员系统</span>
    </div>
    <div class="nav-links">
      <router-link to="/admin/dashboard" class="nav-link">
        <el-icon><Monitor /></el-icon><span>控制台</span>
      </router-link>
      <router-link to="/admin/users" class="nav-link">
        <el-icon><User /></el-icon><span>用户管理</span>
      </router-link>
      <router-link to="/admin/categories" class="nav-link">
        <el-icon><PriceTag /></el-icon><span>分类管理</span>
      </router-link>
      <router-link to="/admin/books" class="nav-link">
        <el-icon><Reading /></el-icon><span>图书管理</span>
      </router-link>
      <router-link to="/admin/reports" class="nav-link">
        <el-icon><Warning /></el-icon><span>举报处理</span>
      </router-link>
      <router-link to="/admin/settings" class="nav-link">
        <el-icon><Setting /></el-icon><span>系统设置</span>
      </router-link>
    </div>
    <div class="user-menu">
      <div class="user-info" @click="toggleDropdown">
        <span class="username">{{ username }}</span>
        <span class="role-badge">管理员</span>
        <el-icon><ArrowDown /></el-icon>
      </div>
      <div class="dropdown-menu" v-show="showDropdown" ref="dropdownMenu">
        <button class="dropdown-item" @click="logout">
          <el-icon><SwitchButton /></el-icon> 退出登录
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import api from '@/api/apiClient'
import {
  Monitor,
  User,
  PriceTag,
  Reading,
  Warning,
  Setting,
  ArrowDown,
  SwitchButton
} from '@element-plus/icons-vue'

const router = useRouter()
const username = ref('管理员')
const showDropdown = ref(false)
const dropdownMenu = ref(null)

// 切换下拉菜单
const toggleDropdown = () => {
  showDropdown.value = !showDropdown.value
}

// 点击外部关闭下拉菜单
const closeDropdownOnClickOutside = (event) => {
  if (showDropdown.value && 
      dropdownMenu.value && 
      !dropdownMenu.value.contains(event.target) && 
      !event.target.closest('.user-info')) {
    showDropdown.value = false
  }
}

onMounted(() => {
  // 获取用户名
  const storedUsername = localStorage.getItem('username')
  if (storedUsername) {
    username.value = storedUsername
  }
  
  // 添加点击外部区域关闭下拉菜单的事件监听
  document.addEventListener('click', closeDropdownOnClickOutside)
})

onBeforeUnmount(() => {
  // 移除事件监听
  document.removeEventListener('click', closeDropdownOnClickOutside)
})

// 退出登录
const logout = async () => {
  try {
    // 调用退出登录API
    await api.user.logout()
  } catch (error) {
    console.error('退出登录时发生错误:', error)
  } finally {
    // 不管API是否成功，都清除本地存储的信息
    localStorage.removeItem('token')
    localStorage.removeItem('userRole')
    localStorage.removeItem('isLoggedIn')
    localStorage.removeItem('userId')
    localStorage.removeItem('username')
    
    // 跳转到登录页
    router.push('/login')
  }
}
</script>

<style scoped>
.ml-1 {
  margin-left: 0.25rem;
}

.admin-navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #1e293b;
  color: white;
  padding: 0.75rem 1.5rem;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.logo {
  display: flex;
  align-items: center;
}

.logo h1 {
  font-size: 1.25rem;
  margin: 0;
  font-weight: 600;
  color: white;
}

.admin-badge {
  background-color: #3b82f6;
  color: white;
  font-size: 0.75rem;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  margin-left: 0.75rem;
}

.nav-links {
  display: flex;
  gap: 1.5rem;
  justify-content: center;
  flex: 1;
}

.nav-link {
  color: #cbd5e1;
  text-decoration: none;
  font-weight: 500;
  padding: 0.5rem 1rem;
  transition: color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  width: 120px;
  gap: 0.5rem;
}

.nav-link:hover, .nav-link.router-link-active {
  color: white;
}

.nav-link .el-icon {
  font-size: 1rem;
}

.nav-link span {
  line-height: 1;
}

.user-menu {
  position: relative;
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.user-info:hover {
  background-color: #2d3748;
}

.username {
  font-weight: 500;
  margin-right: 0.5rem;
}

.role-badge {
  background-color: #8b5cf6;
  color: white;
  font-size: 0.75rem;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  margin-top: 0.5rem;
  z-index: 1000;
  padding: 0.5rem 0;
  min-width: 150px;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  color: #4b5563;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  font-size: 0.875rem;
  white-space: nowrap;
}

.dropdown-item:hover {
  background-color: #f3f4f6;
  color: #1e293b;
}

@media (max-width: 768px) {
  .admin-navbar {
    flex-direction: column;
    align-items: stretch;
    padding: 0.75rem;
  }
  
  .logo {
    margin-bottom: 1rem;
  }
  
  .nav-links {
    overflow-x: auto;
    padding-bottom: 0.5rem;
  }
  
  .user-menu {
    margin-top: 0.75rem;
    justify-content: flex-end;
  }
}
</style>